package com.example.skin_whitening_ndk;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;

import java.io.InputStream;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {
    jni myjni;
    @BindView(R.id.bt_R)
    Button btR;
    @BindView(R.id.bt_G)
    Button btG;
    @BindView(R.id.bt_left)
    Button btLeft;
    @BindView(R.id.bt_Gray)
    Button btRight;
    @BindView(R.id.btn_load)
    Button btnLoad;
    @BindView(R.id.btn_src)
    Button btnSrc;
    @BindView(R.id.btn_java)
    Button btnJava;
    @BindView(R.id.btn_ndk)
    Button btnNdk;
    @BindView(R.id.imageView)
    ImageView imageView;
    @BindView(R.id.linearLayout)
    LinearLayout linearLayout;
    Bitmap dst;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        myjni = new jni();
    }

    @OnClick({R.id.bt_R, R.id.bt_G, R.id.bt_left, R.id.bt_Gray, R.id.btn_load, R.id.btn_src, R.id.btn_java, R.id.btn_ndk})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_R:
                dst = JavaImageRGB.getImageBitmap(src);
                imageView.setImageBitmap(dst);
                break;
            case R.id.bt_G:
                dst = JavaImagePixel.getImageBitmap(src);
                imageView.setImageBitmap(dst);
                break;
            case R.id.bt_left:
                dst = new javaBitmaoRotate().getImageBitmap(src);
                imageView.setImageBitmap(dst);
                break;
            case R.id.bt_Gray:
                dst=javaBitmapToGray.getImageBitmap(src);
                imageView.setImageBitmap(dst);
                break;
            case R.id.btn_load:
                selectImage();
                break;
            case R.id.btn_src:
                imageView.setImageBitmap(src);
                break;
            case R.id.btn_java:
                dst =JavaImageBitmap.getImageBitmap(src);
                imageView.setImageBitmap(dst);
                break;
            case R.id.btn_ndk:
                int srcWidth = src.getWidth();
                int srcHeight = src.getHeight();
                int[] resultInt = myjni.getSkinBitmap(Util.bitmapToArray(src), srcWidth, srcHeight);
                Bitmap dstndk = Util.getJniBitmap(resultInt, srcWidth, srcHeight);
                imageView.setImageBitmap(dstndk);
                break;
        }
    }

    private void selectImage() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "选择图像..."), PICK_IMAGE_REQUEST);
    }

    private double max_size = 1024;
    private int PICK_IMAGE_REQUEST = 1;
    private Bitmap src;

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
            Uri uri = data.getData();
            try {
                Log.d("image-tag", "start to decode selected image now...");
                InputStream input = getContentResolver().openInputStream(uri);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(input, null, options);
                int raw_width = options.outWidth;
                int raw_height = options.outHeight;
                int max = Math.max(raw_width, raw_height);
                int newWidth = raw_width;
                int newHeight = raw_height;
                int inSampleSize = 1;
                if (max > max_size) {
                    newWidth = raw_width / 2;
                    newHeight = raw_height / 2;
                    while ((newWidth / inSampleSize) > max_size || (newHeight / inSampleSize) > max_size) {
                        inSampleSize *= 2;
                    }
                }
                options.inSampleSize = inSampleSize;
                options.inJustDecodeBounds = false;
                options.inPreferredConfig = Bitmap.Config.ARGB_8888;
                src = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
                imageView.setImageBitmap(src);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }


}
